package handler

import (
	"fmt"
	"github.com/antchfx/htmlquery"
	"github.com/gocolly/colly"
	"log"
	"sbd_nyxxw/models"
	"sbd_nyxxw/service"
	"sbd_nyxxw/utils"
	"strings"
)

type CompanyHandler struct {
	CompanySrv service.CompanyService
}

func (handler CompanyHandler) InsterCompanyHandler(viewcp, city string) {
	isCompany := &models.Company{Viewcp: viewcp, City: city}

	// fmt.Println(id)
	url := fmt.Sprintf("https://www.icama.cn/BasicdataSystem/companyquery/viewcompany.do?enterpriseId=%s", viewcp)
	// fmt.Println(url)
	// isCompany := &models.Djzinfo{}
	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"))
	c.OnRequest(func(r *colly.Request) {
		r.Headers.Set("Cookie", "JSESSIONID=A87C2BE548AF94B7CC4B9FA8E40178D0; pageSize=20; pageNo=1")
		// r.Headers.Set("Content-Type", " application/x-www-form-urlencoded")

	})
	c.OnResponse(func(r *colly.Response) {
		doc, err := htmlquery.Parse(strings.NewReader(string(r.Body)))
		if err != nil {
			log.Fatal(err)
		}
		nodes := htmlquery.Find(doc, `/html/body/div[1]/table/tbody/tr`)
		// html/body/div[1]
		for key, node := range nodes {
			if key < 1 {
				continue
			}
			if key == 1 {
				// 单位名称：
				Company := htmlquery.FindOne(node, "./td[2]")
				isCompany.Company = utils.ReplaceStr(htmlquery.InnerText(Company))
				// 省：
				Province := htmlquery.FindOne(node, "./td[4]")
				isCompany.Province = utils.ReplaceStr(htmlquery.InnerText(Province))
				// 国家：
				State := htmlquery.FindOne(node, "./td[6]")
				isCompany.State = utils.ReplaceStr(htmlquery.InnerText(State))
				// fmt.Println(htmlquery.InnerText(Company))
				// fmt.Println(isCompany.State)
			}
			if key == 2 {
				// 县：
				County := htmlquery.FindOne(node, "./td[2]")
				isCompany.County = utils.ReplaceStr(htmlquery.InnerText(County))
				// 乡镇街道：
				Scene := htmlquery.FindOne(node, "./td[4]")
				isCompany.Scene = utils.ReplaceStr(htmlquery.InnerText(Scene))
				// 邮编：
				Code := htmlquery.FindOne(node, "./td[6]")
				isCompany.Code = utils.ReplaceStr(htmlquery.InnerText(Code))
				// fmt.Println(htmlquery.InnerText(Company))
				// fmt.Println(isCompany.Code)
			}
			//
			if key == 3 {
				// 电话：
				Phone := htmlquery.FindOne(node, "./td[2]")
				isCompany.Phone = utils.ReplaceStr(htmlquery.InnerText(Phone))
				// 传真：
				Fax := htmlquery.FindOne(node, "./td[4]")
				isCompany.Fax = utils.ReplaceStr(htmlquery.InnerText(Fax))
				// 联系人：
				Linkman := htmlquery.FindOne(node, "./td[6]")
				isCompany.Linkman = utils.ReplaceStr(htmlquery.InnerText(Linkman))
				// fmt.Println(htmlquery.InnerText(Company))
				// fmt.Println(isCompany.Linkman)
			}
			if key == 4 {
				// 单位地址：
				Address := htmlquery.FindOne(node, "./td[2]")
				isCompany.Address = utils.ReplaceStr(htmlquery.InnerText(Address))
				// E-Mail：
				Mail := htmlquery.FindOne(node, "./td[4]")
				isCompany.Mail = utils.ReplaceStr(htmlquery.InnerText(Mail))

				// fmt.Println(htmlquery.InnerText(Company))
				// fmt.Println(isCompany.Mail)
			}

		}
		isCompany.ID = utils.SnowId()
		handler.CompanySrv.InsterCompanySrv(*isCompany)
	})
	c.Visit(url)
}
